package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.BookingMapper;
import com.bwie.mapper.CliqueMapper;
import com.bwie.mapper.EventsMapper;
import com.bwie.mapper.ProductMapper;
import com.bwie.pojo.Booking;
import com.bwie.pojo.Clique;
import com.bwie.pojo.Events;
import com.bwie.pojo.Product;
import com.bwie.pojo.vo.VoClass;
import com.bwie.service.ProductService;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.rabbitmq.client.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.Message;
import javax.mail.MessagingException;
import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    EventsMapper eventsMapper;

    @Autowired
    BookingMapper bookingMapper;

    @Autowired
    CliqueMapper cliqueMapper;

    @Autowired
    JavaMailSender javaMailSender;

    @Override
    public R prodList(PageInfoVo pageInfoVo) {
        // 构造分页条件
        Page<Product> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        // 查询列表数据
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        // 列表数据
        Page<Product> productPage = productMapper.selectPage(page, wrapper);
        // 返回
        return R.OK(productPage);
    }

    @Override
    public R saveProd(Product product) {
        // 生成商品订单
        String s = RandomUtil.randomNumbers(6);
        // 拼接
        String number = "NS" + s;
        // 设置参数
        product.setProductNumber(number);
        // 使用添加方法
        int insert = productMapper.insert(product);
        // 判断是否添加成功
        if (insert > 0) {
            return R.OK();
        } else {
            return R.ERROR(500, "添加失败");
        }
    }

    @Override
    @Transactional
    public R saveProductAll(VoClass voClass) {
        // 商品列表添加
        Product product = new Product();
        String s = RandomUtil.randomNumbers(6);
        String number = "NS" + s;
        product.setProductNumber(number);
        product.setProductName(voClass.getProductName());
        product.setProductPrice(voClass.getProductPrice());
        product.setProductRepertory(voClass.getProductRepertory());
        // 活动列表添加
        Events events = new Events();
        String ev = RandomUtil.randomNumbers(4);
        String evNumber = "BZ" + ev;
        events.setEventsNumber(evNumber);
        events.setStartTime(voClass.getStartTime());
        events.setEndTime(voClass.getEndTime());
        events.setEventsTitle(voClass.getEventsTitle());
        events.setEventsRepertory(400);
        events.setEventsNoticeRepertory(299);
        events.setStartTime(voClass.getStartTime());
        events.setStartTime(voClass.getStartTime());
        events.setEventsStatus(voClass.getEventsUnmount());
        events.setEventsUnmount(voClass.getEventsUnmount());
        // 成团列表添加
        Booking booking = new Booking();
        booking.setBookingEnd(voClass.getBookingEnd());
        booking.setBookingHandle(voClass.getBookingHandle());
        booking.setBookingNum(voClass.getBookingNum());
        // 实现添加方法
        int prodInsert = productMapper.insert(product);
        int evInsert = eventsMapper.insert(events);
        int bookInsert = bookingMapper.insert(booking);
        if (prodInsert > 0 && evInsert > 0 && bookInsert > 0 ) {
            return R.OK();
        } else {
            return R.ERROR(500, "成团失败");
        }
    }

    @Override
    public R selProdById(Integer id) {
        return R.OK(productMapper.selProdById(id));
    }

    @Override
    public R updBookById(Integer id) {
        return R.OK(bookingMapper.updBookById(id));
    }

    @Override
    public R saveClique(Clique clique) {
        int insert = cliqueMapper.insert(clique);
        if (insert > 0) {
            return R.OK();
        } else {
            return R.ERROR(500, "开团失败");
        }
    }

    @Override
    public R cliqueList() {
        List<Clique> cliques = cliqueMapper.selectList(null);
        return R.OK(cliques);
    }
}
